class Weather

	require 'barometer'
	require 'date'
	
	def Weather.get_weather(city)
			barometer = Barometer.new(city)
			measure = barometer.measure
			forecast = measure.forecast

			location = measure.measurements[0].location
			
			iconFolder = "day"
			
			if measure.night?
				iconFolder ="night"
			end
			
			#user = { "first_name" => "Michael", "last_name" => "Hartl" }
			weather = {	"location" =>
						{
							"city" => city,
							"country" => location.country,
							"latitude" => location.latitude,
							"longitude" => location.longitude,
							"zip_code" => location.zip_code
							
							
						},
						
						"forecast" => 
						{
					
							"now" => {"temperature" => Data::Temperature::f_to_c(measure.current.temperature.fahrenheit).round(0), "weather" => measure.current.icon, "icon"=>"/assets/weather/"+iconFolder+"/"+get_icons[measure.current.icon]},
							"today" => {"day"=>forecast[0].valid_start_date.to_dt.strftime('%a'), "low_temperature" => Data::Temperature::f_to_c(forecast[0].low.fahrenheit).round(0), "high_temperature" => Data::Temperature::f_to_c(forecast[0].high.fahrenheit).round(0), "weather" => forecast[0].icon, "icon"=>"/assets/weather/day/"+get_icons[forecast[0].icon]},
							"tomorrow" => {"day"=>forecast[1].valid_start_date.to_dt.strftime('%a'), "low_temperature" => Data::Temperature::f_to_c(forecast[1].low.fahrenheit).round(0), "high_temperature" => Data::Temperature::f_to_c(forecast[1].high.fahrenheit).round(0),"weather" => forecast[1].icon, "icon"=>"/assets/weather/day/"+get_icons[forecast[1].icon]},
							"day2" => {"day"=>forecast[2].valid_start_date.to_dt.strftime('%a'), "low_temperature" => Data::Temperature::f_to_c(forecast[2].low.fahrenheit).round(0), "high_temperature" => Data::Temperature::f_to_c(forecast[2].high.fahrenheit).round(0), "weather" => forecast[2].icon, "icon"=>"/assets/weather/day/"+get_icons[forecast[2].icon]},
							"day3" => {"day"=>forecast[3].valid_start_date.to_dt.strftime('%a'), "low_temperature" => Data::Temperature::f_to_c(forecast[3].low.fahrenheit).round(0), "high_temperature" => Data::Temperature::f_to_c(forecast[3].high.fahrenheit).round(0), "weather" => forecast[3].icon, "icon"=>"/assets/weather/day/"+get_icons[forecast[3].icon]},
							"day4" => {"day"=>forecast[4].valid_start_date.to_dt.strftime('%a'), "low_temperature" => Data::Temperature::f_to_c(forecast[4].low.fahrenheit).round(0), "high_temperature" => Data::Temperature::f_to_c(forecast[4].high.fahrenheit).round(0), "weather" => forecast[4].icon, "icon"=>"/assets/weather/day/"+get_icons[forecast[4].icon]},
							"day5" => {"day"=>forecast[5].valid_start_date.to_dt.strftime('%a'), "low_temperature" => Data::Temperature::f_to_c(forecast[5].low.fahrenheit).round(0), "high_temperature" => Data::Temperature::f_to_c(forecast[5].high.fahrenheit).round(0), "weather" => forecast[5].icon, "icon"=>"/assets/weather/day/"+get_icons[forecast[5].icon]}
						}
					}
	end
	
	def Weather.get_icons()
		icon =	{
					"cloudy" => "cloudy.png",
					"partlycloudy" => "sun_cloud.png",
					"clear" => "sunny.png",
					"mostlycloudy" => "cloudy.png",
					"rain" => "rain.png",
					"chancesnow" => "snow.png",
					"chancerain" => "sun_rain.png",
					"hazy" => "fog_sun.png",
					"fog" => "fog_rain.png",
					"tstorms" => "storm.png",
					"chancetstorms" => "storm.png",		
					"chanceflurries" => "snow.png",
					"chancerain" => "sun_rain.png",
					"chancesleet" => "snow.png",
					"chancesnow" => "snow.png",
					"chancetstorms" => "storm.png",
					"clear" => "sunny.png",
					"cloudy" => "cloudy.png",
					"flurries" => "snow.png",
					"fog" => "fog_rain.png",
					"hazy" => "fog_sun.png",
					"mostlycloudy" => "cloudy.png",
					"mostlysunny" => "sun_cloud.png",
					"partlycloudy" => "sun_cloud.png",
					"partlycloudy" => "sun_cloud.png",
					"sleet" => "snow.png",
					"rain" => "rain.png",
					"snow" => "snow.png",
					"sunny" => "sunny.png",
					"tstorms" => "storm.png",
					"unknow" => "unknow.png"				
				}
	end
	
	def Weather.to_marker(weather)
	  av = ActionView::Base.new(Rails::Configuration.new.view_path)
	  descriptionMarker = av.render_to_string(:controller => 'weather', :action => 'show', :city => "testdemalade!")
	  return [ 
      { description: descriptionMarker,
         title: weather['location']['city'], 
         sidebar: "", 
         lng: weather['location']['longitude'], 
         lat: weather['location']['latitude'], 
         picture: Weather.get_icons[weather['forecast']['now']['weather']], 
         width: "30", 
         height: "30"} 
     ].to_json
	end
end
